// 上拉刷新，下拉加载
var myscroll = new iScroll("wrapper", {
    onScrollMove: function () {
        if (this.y < (this.maxScrollY)) {
            $('.pull_icon').addClass('flip');
            $('.pull_icon').removeClass('loading');
            $('.more span').text('释放加载...');
        } else {
            $('.pull_icon').removeClass('flip loading');
            $('.more span').text('上拉加载...')
        }
        if (this.y > 30) {
            $(".reload").addClass("reloadShow").text("释放刷新");
        }
    },
    onScrollEnd: function () {
        if ($('.pull_icon').hasClass('flip')) {
            $('.pull_icon').addClass('loading');
            $('.more span').text('加载中...');
            pullUpAction();
        }
        if ($('.reload').hasClass('reloadShow')) {
            document.location.reload();
        }
    },
    onRefresh: function () {
        $('.more').removeClass('flip');
        $('.more span').text('上拉加载...');
    }

});

function pullUpAction() {
    setTimeout(function () {
        /*$.ajax({
            url:'/json/ay.json',
            type:'get',
            dataType:'json',
            success:function(data){
                for (var i = 0; i < 5; i++) {
                    $('.scroller ul').append(data);
                }
                myscroll.refresh();
            },
            error:function(){
                console.log('error');
            },
        })*/
        for (var i = 0; i < 5; i++) {
            $('.scroller ul').append("<li>一只将死之猿!</li>");
        }
        myscroll.refresh();
    }, 1000)
}
if ($('.scroller').height() < $('#wrapper').height()) {
    $('.more').hide();
    myscroll.destroy();
}


//  分类选择
// 根据疾病选医生
$(".category div").click(function (e) {
    e = e || window.event;
    e.stopPropagation();
    var index = $(this).index();
    var flag = $(".panelWrap").attr("flag");
    var positionFlag = $(".positionWrap").attr("flag");
    if (flag == "false") {
        $(".panelWrap").css({ "bottom": "-15rem", "display": "block" }).animate({ "bottom": "0.8rem" });
        $(".panelBox").css({ "transform": 'translateY(0)' });
        $(".panelWrap").attr("flag", "true");
        $(".positionWrap").attr("flag", "true");
    }
    $('.panelWrap .panel' + index)
        .css({
            "display": "block"
        })
        .siblings().css({
            "display": "none"
        });
});

var province = "特色科室";
var city = "普外科特需";
var dataCache = {};

function getCity(lastDataHeight, $parent) {
    var data = [{
        "city": "普外科"
    },
    {
        "city": "普外科特需"
    },
    {
        "city": "心外科"
    },
    {
        "city": "胸外科"
    },
    {
        "city": "胸外科特需"
    },
    {
        "city": "心外科"
    },
    {
        "city": "普外科"
    },
    ];
    dataCache[province] = data;
    move(dataCache[province], lastDataHeight, $parent);
}

function move(data, lastDataHeight, $parent) {
    var html = "<ul>";
    data.forEach(function (item) {
        var li = "<li><a href =''><span >" + item.city + "</span></a></li>";
        html += li;
    });
    html += "</ul>";
    $parent.append(html);
    $parent.animate({ top: -lastDataHeight }, function () {
        $parent.children("ul").eq(0).remove();
        $parent.css({ "top": 0 });
    });
};

// province 
var $parent;
$(".panel .province li").click(function (e) {
    e = e || window.event;
    e.stopPropagation();
    var lastDataHeight = $(this).parent().siblings().find(".city ul").eq(0).height();
    $parent = $(this).parent().siblings().find(".city");
    province = $(this).html();
    $(this).addClass("active").siblings().removeClass("active");
    if (dataCache[province]) {
        move(dataCache[province], lastDataHeight, $parent);
    } else {
        getCity(lastDataHeight, $parent);
    }
});

//  面板消失
function hidenPanelWrap() {
    $(".panelWrap").animate({ "bottom": "-15rem" }, function () {
        $(".panelWrap").css({ "display": "none" });
    });
    // $(".positionWrap").animate({ "top": "20rem" });
    $(".panelWrap").attr("flag", "false");
    $(".panelBox").css({ "transform": 'translateY(100%)' });
    // $(".positionWrap").attr("flag", "false");
}

$(".panel .city").on("click", "li", function () {

    $(this).addClass("active").siblings().removeClass("active");
    city = $(this).children("span").html();
    // $(".currentPositionCity").html(province + " " + city );
    $(".currentPosition span").html(city);
    hidenPanelWrap();
});

$(".panelBox ").click(function () {
    hidenPanelWrap();
});